var showEnergyDetail;
var getEditDialog;
var delTemplate;
var $editDialog;
require(['grid', 'topBar', 'layout', 'date','select2'], function () {
	var postData,carId,$top = $('#top'),$grid = $('#grid');
	var params = $("#top").parent().getValues();
	//左侧树
	//commonFun.leftTreeNodeClickObj['energyDetail'] = carChange;

	function topBarInit() {
		$top.topBar({
			left: [
				{
					text: '省份',
					name: 'provinceId',
					width:160,
					type:'select',
					dict:'ADS00000'
				},
				{

					text: '城市',
					name: 'cityId',
					width:160,
					type:'select',
					
				},
				{
					text: '油价调整日期',
					name: 'adjustDate',
					width:160,
					type: 'select'
				},
				{
					text: '查询',
					type: 'btn',
					click: function () {
						reloadGridData();
					}
				}
				

			],
			right: [
				{
					text: '导出',
					type: 'export-btn',
					authority: '',
					item: [
						{
							text: 'Excel导出',
							click: function () {
								exportFile('Excel');
							}
						},
						{
							text: 'PDF导出',
							click: function () {
								exportFile('Pdf');
							}
						}
						
						
					]
				},
				{
					text: '新增',
					type: 'btn',
					click: function () {
						getEditDialog('新增');
					}
				}
			]
		});
		
		
		
		var $parent = $top.parent();
		var $provinceId = $('select[name="provinceId"]',$parent);
		var $city = $('select[name="cityId"]',$parent);
		//通过选定省份加载城市
		$provinceId.on('change',function(){
			var proviceKey = $('select[name="provinceId"]',$parent).val();
			//从缓存中通过省份的ID找到城市
			var citys = window.parent.GLOBAL_PARAMS.DICT_CACHE[proviceKey]
			var html = "";
			//遍历城市，拼接成下拉选
			$.each(citys,function(index,item){
				html+="<option value='"+item.key+"'>"+item.value+"</option>"
			});
			//将拼接的下拉选加到城市选项中
			$city.html(html);
			//将省份和城市关联,在加载省份的同时重新加载城市
			$city.select2();
			//选择省份时,选中默认的城市
			selectCityInfo();
		});
		//当城市发生改变时
		$city.on('change',function(){
			selectCityInfo();
		});
		
		
	}
	
	function selectCityInfo(){
		//获取页面所有的值
		$.ajaxExt({
			url:"/funEnergyPriceSet/selectEnergyTime.htm",
			data:$("#top").parent().getValues()
		}).done(function(data){
			var adjustTime="";
			var $top = $('#top');
			var $parent = $top.parent();
			var $oilAdjustTime = $('select[name="adjustDate"]',$parent);
			$(data).each(function(i,item){
				adjustTime+="<option value='"+item.adjustDate+"'>"+item.adjustDate+"</option>";
			});
			if(adjustTime==""){
				$oilAdjustTime.html('<option value="" >全部日期</option>');
			}else{
				adjustTime='<option value="" >全部日期</option>'+adjustTime;
				$oilAdjustTime.html(adjustTime);
			}
			$oilAdjustTime.select2();
		});
	}
	

	getAllOilPrice = function(){
		$.ajaxExt({
			url:"/funEnergyPriceSet/getAllOilPrice.htm",
			data:$("#top").parent().getValues()
		}).done(function(){
			
		})
	};
	
	function gridInit() {
		$('#grid').grid({
			multiselect: false,
			url: "/funEnergyPriceSet/searchEnergyInfo.htm",
			colModel: [
			    {name: "id", label: 'id', key: true, hidden: true},
				{name: "provinceId", label: "省份",dict: "ADS00000"},
				{name: "cityId", label: "城市"},
				{name: "adjustDate", label: "调整日期", align: "center"},
				{name: "oilType", label: "燃油种类",align: "center", dict: "FUT00000"},
				{name: "price", label: "燃油价格", align: "right"},
				{name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	getEditDialog('编辑').dialog('exec', 'setVal',data.id);
                        }
                    }, {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                                if (r) {
                                	$.ajaxExt({
            							url : "/funEnergyPriceSet/delEnergyInfo.htm",
            							data : {'id' : data.id}
            						}).done(function(){
            							reload();
            						});
                                }
                            });
                        }
                    }]
            
                }
			]
		});
	}

	reloadGridData = function(){
		var $parent = $top.parent();
		var $provinceId = $('select[name="provinceId"]',$parent);
		if($provinceId.val()!=""){
			var postData = {};
		    postData = $parent.getValues();
			$grid.grid('setGridParam', {
		        datatype: "JSON",
		        page: 1,
		        postData: postData
		    }).trigger("reloadGrid");
		}else{
			$.messager.alert("提示","请在选择省份，再点击查询。","info");
		}
	};
	
	getEditDialog=function(signTxt,fn){
		if(!$editDialog){
	        $editDialog = $('<div>').dialog({
	        	title: signTxt + '能源价格',
	            url: './energy/setting/funEnergySet.htm',
		        height: 320,
		        width: 720,
		        close:function(){
		        	$(this).dialog('exec', 'reset',true);
		        },
	            buttons: [
	          	{
					text : '保存',
					css : 'mr10',
					click : function() {
//						$(this).dialog('exec', 'add',function(){
						$(this).dialog('exec', 'addOrUpdate', function(){
							reload();
	        		    });
						$(this).dialog('close');
					}
				},{
					text : '关闭',
					css : 'btn-cancel',
					click : function() {
						$(this).dialog('close');
					}
				}
	          ]
	    });	
	}
		$editDialog.dialog('open');
		return $editDialog;
	};
	
	//新增修改后执行
	function reload(close){
    	reloadGridData();
    	close && editDialog.dialog('close');
        commonFun.reloadLeftTree();
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			var postData=$top.parent().getValues();
    		$.exportFile("/funEnergyPriceSet/exportEnergyInfo"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }


	//layout 布局
	$('body').layout();
	topBarInit();
	gridInit();
//	$("input[name='beginTime']").val(str)
});